package com.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dao.LoveCarDao;
import com.pojo.LoveCar;
import com.service.LoveCarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author ZhaoJiaDong
 * @version 2020.1
 * @date 2022/11/30 0030 14:31
 */
@Service
public class LoveCarServiceImpl implements LoveCarService{
    @Autowired
    private LoveCarDao loveCarDao;
    @Override
    public List<LoveCar> findByUsername(String username) {
        //新建筛选器
        QueryWrapper<LoveCar> loveCarQueryWrapper = new QueryWrapper<>();
        loveCarQueryWrapper.eq("username",username);
        //条件查询
        List<LoveCar> list = loveCarDao.selectList(loveCarQueryWrapper);
        return list;
    }

    @Override
    public boolean deleteById(Integer id) {
        int count = loveCarDao.deleteById(id);
        return count<1?false:true;
    }

    @Override
    public LoveCar updateById(Integer id,String username) {
        //先查询出来所有，然后设置成0
        QueryWrapper<LoveCar> loveCarQueryWrapper = new QueryWrapper<>();
        loveCarQueryWrapper.eq("username",username);
        List<LoveCar> list = loveCarDao.selectList(loveCarQueryWrapper);
        //遍历修改statu
        System.out.println(id+"---------------------------------");
        for (LoveCar car : list) {
            System.out.println(car.getId()+"-----------------------");
            if (car.getId()==id){
                car.setStatu(1);
            }else{
                car.setStatu(0);
            }
            loveCarDao.updateById(car);
        }
        QueryWrapper<LoveCar> loveCarQueryWrapper1 = new QueryWrapper<>();
        loveCarQueryWrapper1.eq("id",id);
        LoveCar car = loveCarDao.selectOne(loveCarQueryWrapper1);
        return car;
    }

    @Override
    public boolean updateImgById(Integer id,String img) {
        //根据id查出来，然后根据id更改
        QueryWrapper<LoveCar> loveCarQueryWrapper = new QueryWrapper<>();
        loveCarQueryWrapper.eq("id",id);
        LoveCar loveCar = loveCarDao.selectOne(loveCarQueryWrapper);
        loveCar.setImg(img);
        loveCarDao.updateById(loveCar);
        return loveCar==null?false:true;
    }

    @Override
    public boolean add(String username, String newNumber) {
        LoveCar loveCar = new LoveCar();
        loveCar.setUsername(username);
        loveCar.setNumber(newNumber);
        loveCar.setStatu(0);
        int count = loveCarDao.insert(loveCar);
        return count<1?false:true;
    }
}
